﻿@page "/blog/add"

@using Blog.MVP.Blazor.SSR.Models

@inject HttpClient Http
@inject NavigationManager NavManager
@using  Blog.MVP.Blazor.SSR.Pages.Post.component
@inject BlogService BlogService

<h1>添加</h1>

<Editor OnSaveCallback="OnSaveAsync"></Editor>


<div class="text-danger">
    @_errmsg
</div>

@code {

    private BlogArticle BlogArticle { get; set; }

    private string _errmsg;

    protected override Task OnInitializedAsync()
    {
        return base.OnInitializedAsync();
    }

    private async Task OnSaveAsync(BlogArticle blogArticle)
    {
        if (blogArticle.btitle.IsNotEmptyOrNull() && blogArticle.bsubmitter.IsNotEmptyOrNull())
        {

            BlogArticle = blogArticle;

            var result = await BlogService.AddForMVP(blogArticle);

            if (result.IsSuccessStatusCode)
            {
                NavManager.NavigateTo("/blog/list");
            }
            else
            {
                _errmsg = "保存失败! 错误原因：" + result.ReasonPhrase + "。请重试或者登录";
            }
        }
        else
        {
            _errmsg = "参数不能为空！";
        }

    }

}